package Schedule::Chronic::Distributed::Task;

=head1 NAME

  Schedule::Chronic::Distributed::Monitor::Task - Extension of Task for monitor web ui

=head1 SYNOPSIS

  use Schedule::Chronic::Distributed::Monitor::Task;

=head1 DESCRIPTION

This package extends the functionality of Schedule::Chronic::Distributed::Task, by inheriting
from the Maypole Class::DBI Model and providing some extra actions

=head1 METHODS

This package provides the following actions accessible through the Schedule::Chronic::Distributed::Monitor 
Maypole driver.

=cut

use strict;
use base qw(Maypole::Model::CDBI::Plain Schedule::Chronic::Distributed::DBI);


__PACKAGE__->untaint_columns(
           printable => [qw/command_arguments schedule_type current_status last_status description error/],
           integer   => [qw/command current_node last_node last_history min_interval max_interval priority timeout retry_interval category/],
#           date      => [qw/created current_due current_deadline last_due last_deadline last_run last_completed/],
         );


=head2 add_time

Currently only partially implemented, not ready for use yet.

=cut

sub add_time :Exported {
    my ($class,$r) = @_;
    my $task = $class->retrieve($r->{args}->[0]);
    $r->objects([$task]);
    $r->template('view');
}

=head2 running

Populates the running template with a report of the tasks currently running
accross the nodes in the scheduler.

=cut

sub running : Exported {
    my ($class,$r) = @_;
    $r->objects([$class->search(current_status=>'running')]);
    $r->template('running');
}

=head2 overdue

Populates the overdue template with a report of the tasks currently overdue
in the scheduler.

=cut


sub overdue : Exported {
    my ($class,$r) = @_;
    my @objects = $class->retrieve_from_sql(qq{
current_deadline < now()
ORDER BY current_deadline asc
});
    $r->objects([@objects]);
    $r->template('overdue');
}


=head2 errors

Populates the errors template with a report of the tasks currently
with an error status, as well as historical errors.

=cut

sub errors : Exported {
    my ($class,$r) = @_;
    $r->objects([$class->search(current_status => 'error')]);
    $r->template('errors');
}

=head2 warnings

Populates the warnings template with a report of the tasks currently
with a warnings status, as well as historical warnings.

=cut


sub warnings : Exported {
    my ($class,$r) = @_;
    my @objects = $class->retrieve_from_sql(qq{
	warning like '%warning%'
	ORDER BY current_deadline desc
	});
    $r->objects([@objects]);
    $r->template('warnings');
}

=head2 pause

Pauses the given task so that when next due it will not run, this can
be changed later using using the continue action.

=cut

sub pause : Exported {
    my ($class,$r) = @_;
    my $task = $class->retrieve($r->{args}->[0]);
    $task->paused(1);
    $task->update();
    $r->objects( [ $class->retrieve_all() ] );
    $r->template('list');
}

=head2 continue

Sets a paused task to continue, so that when next due or if already
due it will then run.

=cut

sub continue : Exported {
    my ($class,$r) = @_;
    my $task = $class->retrieve($r->{args}->[0]);
    $task->paused(0);
    $task->update();
    $r->objects( [ $class->retrieve_all() ] );
    $r->template('list');
}


###############################################################################

=head1 SEE ALSO

Schedule::Chronic::Distributed::Monitor

Schedule::Chronic::Distributed::Task

Schedule::Chronic::Distributed::Command

=head1 AUTHOR

aaron trevena, E<lt>aaron.trevena@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006 Aaron Trevena, Aviation Briefing Ltd.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5 or,
at your option, any later version of Perl 5 you may have available.


=cut

1;
